﻿using Microsoft.EntityFrameworkCore.Storage;
using SiyinPractice.Framework.Uow;
using System.Threading.Tasks;

namespace SiyinPractice.Infrastructure.EntityFramework.Uow
{
    public class MySqlUnitOfWork : IMySqlUnitOfWork
    {
        private readonly SiyinPracticeMySqlDbContext context;
       // private readonly SiyinPracticeMySqlDbContext mysqlcontext;

        private IDbContextTransaction transaction;

        public MySqlUnitOfWork(SiyinPracticeMySqlDbContext context)
        {
            this.context = context;
           // this.mysqlcontext = mysqlcontext;
        }

        public void Begin()
        {
            //context.beg
            transaction = context.Database.BeginTransaction();
        }
       
        public async Task BeginAsync()
        {
            transaction = await context.Database.BeginTransactionAsync();
        }

        public void Commit()
        {
            transaction.Commit();
        }

        public Task CommitAsync()
        {
            return transaction.CommitAsync();
        }

        public void Dispose()
        {
            if (transaction != null)
            {
                //if (!transaction.WasCommitted && transaction.IsActive)
                //{
                //    transaction.Rollback();
                //}
            }
            transaction?.Dispose();
        }
        //public void MySqlBegin()
        //{
        //    //context.beg
        //    transaction = mysqlcontext.Database.BeginTransaction();
        //}
        //public async Task MySqlBeginAsync()
        //{
        //    transaction = await mysqlcontext.Database.BeginTransactionAsync();
        //}

        //public void MySqlCommit()
        //{
        //    transaction.Commit();
        //}

        //public Task MySqlCommitAsync()
        //{
        //    return transaction.CommitAsync();
        //}

        //public void MySqlDispose()
        //{
        //    if (transaction != null)
        //    {
        //        //if (!transaction.WasCommitted && transaction.IsActive)
        //        //{
        //        //    transaction.Rollback();
        //        //}
        //    }
        //    transaction?.Dispose();
        //}
    }
}